package com.powergisol.core.model;

import com.powergisol.core.math.Dot;
import com.powergisol.gis.geom.GeoPolygon;
import com.vividsolutions.jts.geom.Coordinate;

import java.util.ArrayList;
import java.util.List;

/**
 * 定义三角网
 */
public class TriangleMesh {
    /**
     * 三角网的几何面，主要是针对GIS这块
     */
    private GeoPolygon mesh;

    private List<Coordinate> coordinates;

    private List<PointWithIndex> points;

    private List<Edge> edges;


    public TriangleMesh(List<PointWithIndex> points){


        if(points.size()!=3){
            throw new RuntimeException("三角网有切只能由3个顶点构成");
        }

        this.points = points;
        this.coordinates = new ArrayList<>();
        for (int i = 0; i < points.size(); i++) {

            PointWithIndex pointWithIndex = points.get(i);
            int t = i+1;
            if(t ==points.size()){
                t=0;
            }

            PointWithIndex nextPoint = points.get(t);
            pointWithIndex.setNext(nextPoint);

            Coordinate coordinate = new Coordinate(pointWithIndex.getX(),pointWithIndex.getY(),pointWithIndex.getZ());
            coordinates.add(coordinate);
        }

        init();
    }



    private void init(){

        edges = new ArrayList<>();
        points.forEach(pointWithIndex -> {

            PointWithIndex start = pointWithIndex.getNext();
            PointWithIndex end = start.getNext();
            Edge edge = new Edge(start,end,pointWithIndex);
            edges.add(edge);
            pointWithIndex.setEdge(edge);
        });

        GeoPolygon triangle = new GeoPolygon(coordinates.toArray(new Coordinate[coordinates.size()]));
        this.mesh = triangle;
    }

    public GeoPolygon getMesh() {
        return mesh;
    }

    public List<Coordinate> getCoordinates() {
        return coordinates;
    }


    public List<PointWithIndex> getPoints() {
        return points;
    }

    /**
     * 三角形的点是否倒序
     */
    public boolean isReverse(){
        List<Coordinate> coordinates = getCoordinates();

        coordinates.forEach(coordinate -> {


        });

        return false;
    }


    @Override
    public String toString() {
        return "TriangleMesh{" +
                "points=" + points +
                '}';
    }
}
